package com.yunmai.library.util;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

/**
 * {@code StatusBar} 操作工具类
 *
 *
 * <p>Created by littleGnAl on 2016/1/22.
 */
public class StatusBarUtil {

  /**
   * 给{@code view}设置全屏标志 {@link View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}和
   * {@link View#SYSTEM_UI_FLAG_LAYOUT_STABLE}
   * <p>See: {@link View#setSystemUiVisibility(int)}
   */
  public static boolean setViewFullScreen(View view) {
    if (canViewSetFullScreen()) {
      view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
      view.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
      return true;
    }

    return false;
  }

  /**
   * 获取{@code StatusBar}的高度
   */
  public static int getStatusBarHeight(Context context) {
    int result = 0;
    int resourceId = context.getResources().getIdentifier(
        "status_bar_height",
        "dimen", "android");
    if (resourceId > 0) {
      result = context.getResources().getDimensionPixelSize(resourceId);
    }
    return result;
  }

  /**
   * 设置透明状态栏
   * sdk版本是否4.4之后才可以
   *
   * @param activity 需要设置透明状态栏的{@code activity}
   * @return 如果设置成功则返回<code>true</code> 否则<code>false</code>
   */
  public static boolean setStatusBarMode(Activity activity) {
    return setStatusBarMode(activity, false);
  }

  public static boolean setStatusBarMode(Activity activity, boolean statusBarTextDark) {
    return setStatusBarMode(activity, activity.getResources().getColor(android.R.color.transparent), statusBarTextDark);
  }

  public static boolean setStatusBarMode(Activity activity, @ColorInt int color, boolean statusBarTextDark) {
    if (activity == null) return false;
    Window window = activity.getWindow();
    if (window == null) return false;
    if (Build.VERSION.SDK_INT >= 23) {
      setStatusBarColor(window,color);
      if (statusBarTextDark) {
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
      }
      return true;
    } else if (Build.VERSION.SDK_INT >= 19) {
      return setStatusBarTransparent(activity);
    }
    return false;
  }

  /**
   * 设置<code>StatusBar</code>为透明
   *
   * @param activity {@link Activity}
   * @return 设置成功返回 <code>true</code>, 否则返回<code>false</code>
   */
  private static boolean setStatusBarTransparent(Activity activity) {
    if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT < 21) {
      //return setStatusBarMode(activity);
      activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      return true;
    } else if (Build.VERSION.SDK_INT >= 21) {
      return setStatusBarColor(
          activity,
          activity.getResources().getColor(android.R.color.transparent));
    }
    return false;
  }

  /**
   * sdk版本>=21({@code Lolipop}) 设置{@code StatusBar}颜色
   *
   * @param activity {@link Activity}
   * @param color 颜色
   */
  private static boolean setStatusBarColor(Activity activity, @ColorInt int color) {
    return setStatusBarColor(activity.getWindow(), color);
  }

  private static boolean setStatusBarColor(Window window, @ColorInt int color) {
    if (window == null) return false;
    if (Build.VERSION.SDK_INT >= 21) {
      setViewFullScreen(window.getDecorView());
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      window.setStatusBarColor(color);

      return true;
    }

    return false;
  }
  /**
   * sdk>=16 可以对{@link View}设置全屏(See: {@link #setViewFullScreen(View)}) 但是只有4.4后设置透明状
   * 态栏全屏才是理想效果 所以这里直接直接设定为sdk版本>=19才能设置全屏
   */
  private static boolean canViewSetFullScreen() {
    if (Build.VERSION.SDK_INT >= 19) {
      return true;
    }
    return false;
  }

}
